package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@RestController
@RequestMapping("order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("");
    }

    /**
     * 订单信息分页查询
     * @param page
     * @param pageSize
     * @param
     * @return
     */
    @GetMapping("userPage")
    public R<Page> page(int page,int pageSize){
        log.info("page={},pageSize={}",page,pageSize);

        Orders orders=new Orders();
        //构造分页构造器
        Page pageInfo = new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.eq(Orders::getUserId,BaseContext.getCurrentId());
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //执行查询
        orderService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    @GetMapping("page")
    public R<Page> page(Integer page, Integer pageSize, Long number, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        log.info("page={},pageSize={},number={},beginTime={},endTime={}",page,pageSize,number,beginTime,endTime);
        //构造分页构造器对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(number != null, Orders::getNumber,number);
        //大于等于开始时间 ge
        queryWrapper.ge(beginTime != null,Orders::getOrderTime,beginTime);
        //小于等于结束时间 le
        queryWrapper.le(endTime != null,Orders::getCheckoutTime,endTime);
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getOrderTime);
        //执行分页查询
        orderService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    //修改配送状态
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        //订单状态 1待付款，2待派送，3已派送，4已完成，5已取消 Status
        log.info("orders={}",orders);
        //构造查询条件
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        //获取前台传过来id和数据库id比较
        queryWrapper.eq(Orders::getId,orders.getId());
        //获取一条数据
        Orders orderServiceOne = orderService.getOne(queryWrapper);
        //用前端传过来的订单状态,改变数据库的订单状态
        orderServiceOne.setStatus(orders.getStatus());
        //条件构造器作为参数进行更新,根据id更新
        //updateById方法在插入时，会根据实体类的每个属性进行非空判断，
        // ,只有非空的属性所对应的字段才会出现在SQL语句中。
        orderService.updateById(orderServiceOne);
        return R.success("修改订单状态成功");
    }




    //再来一单
    @PostMapping("/again")
    public R<String> againById(@RequestBody Orders orders){
        log.info("id={}",orders);
        //获取当前登录用户id
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<OrderDetail> queryWrapper=new LambdaQueryWrapper<>();
        //订单id
        queryWrapper.eq(OrderDetail::getOrderId,orders.getId());
        List<OrderDetail> list = orderDetailService.list(queryWrapper);

        List<ShoppingCart> shoppingCarts= list.stream().map(item->{
            ShoppingCart shoppingCart=new ShoppingCart();

            BeanUtils.copyProperties(item,shoppingCart);

            shoppingCart.setUserId(currentId);
            shoppingCart.setId(shoppingCart.getId());
            return shoppingCart;
        }).collect(Collectors.toList());

        shoppingCartService.saveBatch(shoppingCarts);
        return R.success("再来一单");
    }

}
